package com.woniuxy.controller;


import com.github.pagehelper.PageInfo;
import com.woniuxy.annotation.MyLog;
import com.woniuxy.entity.Indent;
import com.woniuxy.entity.User;

import com.woniuxy.service.indent.IndentService;
import com.woniuxy.util.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class IndentController {
    @Autowired
    private IndentService indentService;


    @PostMapping("indent/findAll")
    public ResultVo findAll() {

        List<Indent> list = indentService.findAll();
        return ResultVo.success(list);
    }

    @MyLog("故障上报")
    @PostMapping("indent/update")
    public ResultVo update(@RequestBody Indent indent) {
//        System.out.println(indent.toString());
        int rows = indentService.update(indent);
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }

    //TODO 修改表结构后，该功能无法使用
    @MyLog("生成订单")
    @PostMapping("indent/save")
    public ResultVo save(@RequestBody Indent indent, HttpServletRequest request) {
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        indent.setUserId(loginUser.getUserId());
        int rows = indentService.save(indent);
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }

    @MyLog("订单删除")
    @PostMapping("indent/delete")
    public ResultVo delete(@RequestBody String indentId) {
        if (!StringUtils.hasText(indentId)) {
            throw new RuntimeException("indentId is required");
        }
        int rows = indentService.delete(Integer.valueOf(indentId));
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }

    @PostMapping("indent/findByPage")
    public ResultVo findByPage(@RequestBody Map<String, String> map) {
        String page = map.remove("page");
        String size = map.remove("size");
        PageInfo<Indent> list = indentService.findByPage(Integer.valueOf(page), Integer.valueOf(size));
        return ResultVo.success(list);
    }

    //TODO 修改表结构后，该功能无法使用
    @PostMapping("indent/returnBed")
    public ResultVo returnBed(@RequestBody Indent indent, HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        indent.setUserId(loginUser.getUserId());
        int rows = indentService.returnBed(indent);
        return rows>0? ResultVo.success():ResultVo.failure();
    }
    //TODO 修改表结构后，该功能无法使用
    @PostMapping("indent/pay")
    public ResultVo pay(@RequestBody Indent indent,HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        indent.setUserId(loginUser.getUserId());
        int rows = indentService.pay(indent);
        return rows>0? ResultVo.success():ResultVo.failure();
    }
}
